package main

import (
	"log"
	"project/config"
	"github.com/streadway/amqp"
	)

func main()  {
	var queueName = config.RabbitMqConf.QueueName
	var delayQueueName = config.RabbitMqConf.DelayQueueName

	conn, err := amqp.Dial(config.RabbitMqConf.Addr)
	if err != nil {
		log.Fatalf("%s: %s", "Failed to connect to RabbitMQ", err)
	}
	defer conn.Close()

	ch, err := conn.Channel()
	if err != nil {
		log.Fatalf("%s: %s", "Failed to open channel", err)
	}
	defer ch.Close()

	// 声明一个主要使用的 exchange
	err = ch.ExchangeDeclare(
			"delayExchange",	//交换机名称
			"direct",
			true,
			false,
			false,
			false,
			nil,
		)
	if err != nil {
		log.Fatalf("%s: %s", "Failed to declare an exchange", err)
	}

	//声明一个常规队列，不声明会exchange默认绑定一个
	q, err := ch.QueueDeclare(
			queueName,
			true,
			false,
			false,
			false,
			nil,
		)
	if err != nil {
		log.Fatalf("%s: %s", "Failed to declare an queue", err)
	}

	//队列与交换机绑定
	err = ch.QueueBind(
		q.Name,
		delayQueueName,
		"delayExchange",
		false,
		nil,
		)

	if err != nil {
		log.Fatalf("%s: %s", "Failed to bing exchange", err)
	}

	//消费
	msgs, err := ch.Consume(
		queueName,
		"",
		false,
		false,
		false,
		false,
		nil)

	if err != nil {
		log.Fatalf("%s: %s", "Failed to bing exchange", err)
	}

	//处理消费逻辑
	go func() {

	}()
}
